$(document).bind("ready", function() {
    
    var validationMessages = [], $list = $("#msgerro > ul");
	
    $(document).trule({
        messages: window.messages,
        firstErro: function(r) {
            try {
                r.oRef.focus();
            } catch (ex) {
            }
        },
        error: function(r, msg) {
            validationMessages.push(msg+'.');
        },
        lastErro: function(r) {
            //habilita o elemento que mostra os erros
            $("#msgerro").show();
            // Junta as mensagens e faz a saída
            $list.empty().html("<li>".concat(validationMessages.join("</li><li>"), "</li>"));
            //$('.boxError').show();
            // Reseta mensagens
            validationMessages = [];
        }
    });

    $("form").bind("submit", function(e) {
        if ($(this).trule().validateAll() === false) {
            e.preventDefault();
        } else
            $("select[class='validateRequired'] option[value='']").attr("value", '0');
    });
    $("select option[value='0']").attr("value", '');


    //tratar data com apenas dia e mes
    var validateDataDM = {
        name: "validateDataDM",
        event: "submit",
        rule: function(r) {
            var
            $this = $(this),
            splittedEntry = $this.val().match(/^(\d{2})\/(\d{2})$/),
            day, month;
            if (splittedEntry !== null) {
                day = parseInt(splittedEntry[1], 10);
                month = parseInt(splittedEntry[2], 10);

                if (month >= 1 && month <= 12) {
                    if (day >= 1) {
                        switch (month) {
                            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                                if (day <= 31) {
                                    return r.firesSuccess();
                                }
                                break;

                            case 4: case 6: case 9: case 11:
                                if (day <= 30) {
                                    return r.firesSuccess();
                                }
                                break;

                            case 2:
                                if (day <= 29) {
                                    return r.firesSuccess();
                                }
                                break;
                        }
                    }
                }
                return r.firesError("dataDMInvalid", {
                    fieldname: r.showAs
                });
            }

        //return r.firesError("dataDMInvalid", {fieldname: r.showAs});
        },
        messages: {
            "pt-br": {
                dataDMInvalid: "{fieldname} não está com um formato válido DD/MM/YYYY"
            }
        },
        mask: "99/99/9999"
    };
    $(".validateDataDM").trule().addRule(validateDataDM);

	
});
